package ch.fusun.baron.player.server.ui;

import java.util.List;

import org.eclipse.swt.graphics.Image;

import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.core.rmi.User;
import ch.fusun.baron.data.DataListener;
import ch.fusun.baron.data.DataUpdate;
import ch.fusun.baron.player.Gender;
import ch.fusun.baron.player.Player;
import ch.fusun.baron.player.api.PlayerService;
import ch.fusun.baron.serverapp.view.UserChildrenProvider;

/**
 * Provides all players
 */
public class PlayerChildrenProvider implements UserChildrenProvider,
		DataListener {

	private PlayerService playerService;

	/**
	 * Injection constructor
	 */
	public PlayerChildrenProvider() {
	}

	/**
	 * @param playerService
	 *            The new service
	 */
	@Inject
	public void setPlayerService(PlayerService playerService) {
		this.playerService = playerService;
		playerService.addDataListener(this);
	}

	@Override
	public List<?> getChildren(User user) {
		return playerService.getPlayers(user);
	}

	@Override
	public Image getImage(Object obj) {
		if (obj instanceof Player) {
			Player player = (Player) obj;
			if (player.getAge() < 5) {
				return Activator.getImage(Activator.BABY);
			}
			if (Gender.MALE.equals(player.getGender())) {
				if (player.getAge() < 16) {
					return Activator.getImage(Activator.BOY);
				}
				return Activator.getImage(Activator.MAN);
			} else if (Gender.FEMALE.equals(player.getGender())) {
				if (player.getAge() < 16) {
					return Activator.getImage(Activator.GIRL);
				}
				return Activator.getImage(Activator.WOMAN);
			}

		}
		return null;
	}

	@Override
	public void dataChanged(DataUpdate update) {
		// Do nothing, we need to register as listeners, so we get updated if
		// the services change
	}
}
